package com.lanhao.aot.elasticsearch;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;

@Configuration
@ConfigurationProperties(prefix = "smt.elasticsearch")
public class ElasticsearchConfig {
	
	@Bean
	public ElasticsearchClient elasticsearchClient() {
		RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
		// 使用Jackson映射器创建传输层
		ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
		// 创建API客户端
		ElasticsearchClient client = new ElasticsearchClient(transport);

		// 关闭ES客户端
//      transport.close();
//      restClient.close();
		return client;
	}
	
}
